App.controller('reimburseBillController', ['$rootScope', '$scope', '$state', 'Page','XCode', function($rootScope, $scope, $state, Page,XCode) {

    //部门查询
    Page.ajaxPost('department/treeGrid.do', null, function (resp) {
        $scope.depts = resp.data;
    });

    //员工查询
    Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
        $scope.empList = resp.data;
    });


    $scope.grid = {
        options: {
            pageLength : 5,
            //draw()回调
            drawCallback : function(){
                setTimeout(function(){
                    $scope.initChecked();
                },100)
            },
            select :{
                style : 'single',
                onSelect : function(rows) {
                    if(rows[0]._index != 1) {
                        if($("#table1 tbody tr[_index='1']").hasClass("selected")) {
                            $("#table1 tbody tr[_index='1']").removeClass("selected")
                        }
                    } else {
                        $("#table1 tbody tr[_index='1']").addClass("selected");
                    }

                    currentSelected = rows[0]._index;
                    lastSelected = $scope.lastSelected;
                    if(currentSelected == lastSelected){
                        $("#table1 tbody tr[_index= "+ currentSelected + "]").addClass("selected");
                    }else{
                        $("#table1 tbody tr[_index= "+ lastSelected + "]").removeClass("selected");
                        $scope.lastSelected = currentSelected;
                    }
                    $scope.selectRow = rows[0];
                    $scope.reimburseDataQuery($scope.selectRow);
                }
            }
        }
    };

    $scope.reimburseGrid = {
        options : {
            lazy : true
        },
        add : function() {
            $scope.reimburseBill = {};
            $scope.modal.show();
        },
        update : function(row) {
            $scope.reimburseBill = row;
            $scope.reimburseBill.id = row.empId;
            $scope.modal.show();
        },
        view : function(row) {
            $scope.reimburseView = row;
            $scope.viewModal.show();
        },
        save : function() {
            $scope.validateForm.doValidate();
            if($scope.validateForm.$valid) {
                var pattern = /^\d+(\.\d*)?$/;
                if(!pattern.test($scope.reimburseBill.totalAmount)) {
                    Page.notice("请输入正确的单价!");
                    return;
                }
                $scope.reimburseBill.orderId = $scope.selectRow.orderId;
                $scope.reimburseBill.empId = $scope.reimburseBill.id;
                Page.ajaxPost($scope.reimburseBill.reimburseId ? 'reimburseBill/update.do' : 'reimburseBill/add.do', $scope.reimburseBill, function() {
                    Page.notice("操作成功!");
                    $scope.reimburseGrid.cancel();
                    $scope.reimburseGrid.query({'orderId':$scope.selectRow.orderId});
                });
            }
        },
        cancel : function() {
            $scope.reimburseBill = {};
            $scope.modal.hide();
        },
        column_defs: [
            {
                target : 7,
                template : function(row) {
                    var html = '';
                    if(row.status == '1' || row.status == '4') {
                        html = '<span style="color : red">' + XCode.getName('reimburse_status',row.status) + '</span>';
                    } else if(row.status == '3') {
                        html = '<span style="color : green">' + XCode.getName('reimburse_status',row.status) + '</span>';
                    } else {
                        html = '<span style="color : orange">' + XCode.getName('reimburse_status',row.status) + '</span>';
                    }
                    return html;
                }
            },
            {
                target : 8,
                buttons : [
                    {
                        name: '编辑',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'update',
                        visiable : function(row) {
                            if(row.status == "1" || row.status == "4") {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    },
                    {
                        name: '删除',
                        class: 'btn-danger',
                        icon: 'fa-close',
                        event: function(row) {
                            Page.confirm("确定要删除吗？", function() {
                                Page.ajaxPost('reimburseBill/delete.do', row, function() {
                                    $scope.reimburseGrid.query({'orderId':$scope.selectRow.orderId});
                                });
                            });
                        },
                        visiable : function(row) {
                            if(row.status == "1" || row.status == "4") {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    },
                    {
                        name: '提交审核',
                        class: 'btn-info',
                        icon: 'glyphicon glyphicon-exclamation-sign',
                        event: function(row) {
                            Page.confirm("确定要提交审核吗？", function() {
                                Page.ajaxPost('reimburseBill/apply.do', row, function() {
                                    Page.notice("操作成功!");
                                    $scope.reimburseGrid.query({'orderId' : $scope.selectRow.orderId});
                                });
                            });
                        },
                        visiable : function(row) {
                            if(row.status == "1" || row.status == "4") {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    },
                    {
                        name: '查看',
                        class: 'btn-info',
                        icon: 'glyphicon glyphicon-eye-open',
                        event: 'view',
                        visiable : function(row) {
                            if(row.status != '1' && row.status != '4') {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    }
                ]
            }
        ]
    };


    $scope.selectable = function(data) {
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }
    };

    $scope.$on('$viewContentLoaded', function() {
        $('#table1 tbody').on( 'click', 'tr', function () {
            if ( $(this).hasClass('selected') ) {
            }
            else {
                $(this).addClass('selected');
            }
        });
    });

    $scope.initChecked = function() {
        $("#table1 tbody tr[_index='1']").addClass("selected");
        $scope.selectRow = {};
        $("#table1 tbody tr[_index='1']").find("td").each(function(index,element){
            if(index == 1) {
                $scope.selectRow.orderId = $(element).html();
            }
        });

        $scope.reimburseDataQuery($scope.selectRow);
    };

    $scope.reimburseDataQuery = function(row) {
        if(!$scope.showReimburseFlag) {
            $scope.showReimburseFlag = true;
        }
        $scope.reimburseGrid.query({"orderId" : row.orderId});
    };

    $scope.sure = function() {
        $scope.viewModal.hide();
    }

}]);